<?php
use yii\helpers\Html;
use common\models\User;

/* @var $this \yii\web\View */
/* @var $content string */
if (Yii::$app->controller->action->id === 'login') {

    /**
     * Do not use this code in your template.
     * Remove it.
     * Instead, use the code $this->layout = '//main-login'; in your controller.
     */
    echo $this->render ( 'main-login', [
        'content' => $content
    ] );
} else {
    $allow = [//为允许进入的文件夹，不会被路由掉
        "uploads/yuyin/",
        "uploads/freeswitch/",
        "downloads/cti/"
    ];
    $res = 0;
    foreach ($allow as $k=>$v){
        if(strpos(Yii::$app->requestedRoute,$v) === true){
            $res++;
        };
    }
    if (Yii::$app->user->getIsGuest() && (Yii::$app->controller->action->id != 'login' && $res==0) ){ // 没登录就直接跳转到登录页面
        $login_url = Yii::$app->getHomeUrl().'site/login';
        Yii::$app->getResponse()->redirect($login_url);
    }
    if (class_exists ( 'backend\assets\AppAsset' )) {
        backend\assets\AppAsset::register ( $this );
    } else {
        app\assets\AppAsset::register ( $this );
    }

    dmstr\web\AdminLteAsset::register ( $this );

    $directoryAsset = Yii::$app->assetManager->getPublishedUrl ( '@vendor/almasaeed2010/adminlte/dist' );

    $user = Yii::$app->user;
    $username = '';
    $is_logining = '';
    $role_name = '';
    if (! empty ( $user->identity )) {
        $username = Yii::$app->user->identity->username;
        $user_id = Yii::$app->user->identity->id;
        $is_logining = Yii::$app->user->isGuest;
        if ($is_logining) {
            $is_logining = '离线';
        } else {
            $is_logining = '在线';
        }
        $role = Yii::$app->authManager->getRolesByUser ( $user_id );
        $role = end ( $role );
        $role_name = $role->name;
    }
    ?>
    <?php $this->beginPage()?>
    <!DOCTYPE html>
    <html lang="<?= Yii::$app->language ?>">
    <head>
        <meta charset="<?= Yii::$app->charset ?>" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?= Html::csrfMetaTags()?>
        <title><?= Html::encode($this->title) ?></title>
        <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
        <script>
            //设置cookie
            function setCookie(c_name,value,expiredays)
            {
                var exdate=new Date()
                exdate.setDate(exdate.getDate()+expiredays)
                document.cookie=c_name+ "=" +escape(value)+
                    ((expiredays==null) ? "" : ";path=/;expires="+exdate.toGMTString())
            }
            //拿到cookie
            function getCookie(c_name)
            {
                if (document.cookie.length>0)
                {
                    c_start=document.cookie.indexOf(c_name + "=")
                    if (c_start!=-1)
                    {
                        c_start=c_start + c_name.length+1
                        c_end=document.cookie.indexOf(";",c_start)
                        if (c_end==-1) c_end=document.cookie.length
                        return unescape(document.cookie.substring(c_start,c_end))
                    }
                }
                return ""
            }
        </script>
        <?php $this->head()?>
    </head>
    <style>
        .my-content{
            background-color: #FFFFFF;
            padding: 9px 9px 4px;
            margin: 9px 9px 4px;
            background-color: #fff;
            border: 1px solid #ddd;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            border-radius: 4px;
        }
        /*所有的div的滚动条的样式*/
        div::-webkit-scrollbar {
            width: 8px;
            height: 8px;
            background-color: #f5f5f5;
        }
        div::-webkit-scrollbar-thumb {
            border-radius: 10px;
            -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
            background-color: #ccc;
        }
        div::-webkit-scrollbar-track {
            -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
            border-radius: 10px;
            background-color: #f5f5f5;
        }
        .my-well {
            min-height: 20px;
            padding: 19px;
            background-color: #ffffff;
            border: 1px solid #e3e3e3;
            border-radius: 8px;
            -webkit-box-shadow: inset 0 4px 4px rgba(0, 0, 0, .1);
            box-shadow: inset 0 4px 4px rgba(0, 0, 0, .1);
        }
    </style>
    <body id="zhankaibody" class="skin-blue sidebar-mini sidebar-collapse">
    <script>
        //以下代码实现关闭与开启的记录
        if ( getCookie("zhankai")==""||getCookie("zhankai")==undefined) {
            setCookie("zhankai","skin-blue sidebar-mini sidebar-collapse",30);
            $("#zhankaibody").attr("class","skin-blue sidebar-mini sidebar-collapse");
        }else{
            $("#zhankaibody").attr("class",getCookie("zhankai"));
        }
    </script>
    <?php $this->beginBody()?>
    <!-- 顶部的文件加载如果设置了$top=flase就不显示 -->
    <div class="wrapper" >

        <?=$this->render ( 'header.php', [ 'directoryAsset' => $directoryAsset,'username' => $username,'is_logining' => $is_logining,'role_name' => $role_name ] )?>

        <?=$this->render ( 'left.php', [ 'directoryAsset' => $directoryAsset ,'username' => $username,'is_logining' => $is_logining,'role_name' => $role_name] )?>

        <?=$this->render ( 'content.php', [ 'content' => $content,'directoryAsset' => $directoryAsset ] )?>

    </div>

    <?php $this->endBody()?>
    <script>
        $("#zhankai").click(function () {
            if($("#zhankaibody").attr("class")=="skin-blue sidebar-mini sidebar-collapse"){
                setCookie("zhankai","skin-blue sidebar-mini",30);
            }else{
                setCookie("zhankai","skin-blue sidebar-mini sidebar-collapse",30);
            }
        });
    </script>
    <script>
        $(".menu-open").bind("click",function () {
            $(this).removeClass("menu-open");
        });
    </script>
    </body>
    </html>
    <?php $this->endPage()?>
<?php } ?>